<template>
  <div class="upload">
    <p class="title">人员管理</p>
    <hr/>
    <el-button type="primary" @click="showInviteDialog">邀请人员</el-button>
      <el-table
              :data="tableData"
              style="width: 100%">
        <el-table-column
                type="index"
                label="序号"
                width="400">
        </el-table-column>
        <el-table-column
                prop="name"
                label="姓名"
                width="400">
        </el-table-column>
        <el-table-column
                label="操作"
                width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="removeHr(scope.row)">移除</el-button><!--v-show="scope.row.id!=1"-->
          </template>
        </el-table-column>
      </el-table>
      
    <!-- 邀请人员对话框 -->
    <el-dialog
      title="邀请人员"
      :visible.sync="inviteDialogVisible"
      width="40%">
      <el-form :model="inviteForm" :rules="inviteRules" ref="inviteForm" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="inviteForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="inviteForm.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="inviteForm.sex">
            <el-radio label="0">男</el-radio>
            <el-radio label="1">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="inviteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="inviteHr">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import {cities} from "../../../assets/data/form";

export default {
  name: 'PersonManage',
  data () {
    return {
      tableData: [],
      inviteDialogVisible: false,
      inviteForm: {
        username: '',
        name: '',
        sex: '0',
        recruitUnitId: 0
      },
      inviteRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created () {
    this.getRecruitUnitId();
    this.queryHrList();
  },
  methods: {
    // 获取招聘单位ID
    getRecruitUnitId() {
      this.$axios.post("/recruitUnit/getOne", {}).then(res => {
        if (res.code === 200) {
          this.inviteForm.recruitUnitId = res.data.id;
        }
      });
    },
    
    // 查询HR列表
    queryHrList() {
      this.$axios.post("/hrInfo/list", {}).then(res => {
        if (res.code === 200) {
          this.tableData = res.data.records;
        } else {
          this.$message.error(res.msg || '获取人员列表失败');
        }
      });
    },
    
    // 显示邀请对话框
    showInviteDialog() {
      this.inviteForm = {
        username: '',
        name: '',
        sex: '0',
        recruitUnitId: this.inviteForm.recruitUnitId
      };
      this.inviteDialogVisible = true;
    },
    
    // 邀请HR
    inviteHr() {
      this.$refs.inviteForm.validate(valid => {
        if (valid) {
          // 先检查用户是否存在
          this.$axios.post("/hrInfo/getOne", { username: this.inviteForm.username }).then(res => {
            if (res.code === 200) {
              // 用户已存在，直接添加关联
              const hrInfo = {
                userId: res.data.userId,
                name: this.inviteForm.name,
                sex: this.inviteForm.sex,
                recruitUnitId: this.inviteForm.recruitUnitId
              };
              
              this.addHrToUnit(hrInfo);
            } else {
              // 用户不存在，创建用户并添加关联
              this.$confirm('用户不存在，是否创建新用户?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                // 创建用户
                const userInfo = {
                  username: this.inviteForm.username,
                  password: '123456', // 默认密码
                  type: 2 // HR类型
                };
                
                this.$axios.post("/user/register", userInfo).then(regRes => {
                  if (regRes.code === 200) {
                    // 创建用户成功，添加HR信息
                    const hrInfo = {
                      userId: regRes.data.id,
                      name: this.inviteForm.name,
                      sex: this.inviteForm.sex,
                      recruitUnitId: this.inviteForm.recruitUnitId
                    };
                    
                    this.addHrToUnit(hrInfo);
                  } else {
                    this.$message.error(regRes.msg || '创建用户失败');
                  }
                });
              }).catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消创建用户'
                });
              });
            }
          });
        }
      });
    },
    
    // 添加HR到单位
    addHrToUnit(hrInfo) {
      this.$axios.post("/hrInfo/add", hrInfo).then(res => {
        if (res.code === 200) {
          this.$message.success('邀请人员成功');
          this.inviteDialogVisible = false;
          this.queryHrList(); // 刷新列表
        } else {
          this.$message.error(res.msg || '邀请人员失败');
        }
      });
    },
    
    // 移除HR
    removeHr(row) {
      this.$confirm('确定要移除该人员吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 修改HR信息，解除与单位的关联
        const hrInfo = {
          id: row.id,
          recruitUnitId: null
        };
        
        this.$axios.post("/hrInfo/edit", hrInfo).then(res => {
          if (res.code === 200) {
            this.$message.success('移除人员成功');
            this.queryHrList(); // 刷新列表
          } else {
            this.$message.error(res.msg || '移除人员失败');
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消移除'
        });
      });
    }
  }
}
</script>

<style lang="scss" scoped>
@use 'src/assets/stycle-zhou/upload';
</style>
